const {fork} = require('child_process');
const macroTasks = [];

class XMLHttpRequest {
  constructor() {
    this.options = {};
  }

  open(method, url) {
    this.options.method = method;
    this.options.url = url;
  }

  send(){
    const child = fork('./XMLHttpRequest.js');
    child.on('message', (message) => {
      if (message.type === 'response') {
        this.response = message.data;
        macroTasks.push(this.onload);
      }
    });
    child.send({type: 'send', options: this.options});
  }
}

function fetchData() {
  const xhr = new XMLHttpRequest();
  xhr.open('GET', 'http://localhost:3000/data');
  xhr.onload = function () {
    console.log(xhr.response);
  };
  xhr.send();
}

console.log(1);
fetchData();
console.log(2);


setInterval(() => {
  const macroTask = macroTasks.shift();
  macroTask && macroTask();
},100);
